home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Megarom
/
Megarom Macintosh CD Software (Quantum Leap)(1992).iso
/
SOUND
/
Applications
/
Sound Input Device ƒ
/
SID Test Util.c
< prev
Wrap
C/C++ Source or Header
|
1990-01-05
|
19KB
|
736 lines
/* ************ SID Test Util.c - A test utility for SID *************** */
/* */
/* Copyright (C) 1990 */
/* The SID Trio */
/* All Rights Reserved */
/* */
/* */
/* ********************************************************************* */
/****
* Note:
* This purpose of this program is to 1) assist in the testing of SID
* hardware, and to 2) give an example of using the HearHere toolbox.
* This application uses a fixed 100K buffer (size set by the constant
* BUFFERSIZE) - real applications should always use as much RAM as
* possible for quality recording.
*
* Warning:
* Don't set the front and rear windows until you understand what they do.
* Setting these windows and turning off "Interrupt on mouse" can lock up
* your Mac pretty easily!
*
*/
#include "HearHere.h"
#include <SoundMgr.h>
#include <SoundDvr.h>
/* *** Menu Definitions *** */
#define APPLE_MENU 201
#define ABOUT_ITEM 1
#define FILE_MENU 202
#define QUIT_ITEM 1
#define RECORD_MENU 203
#define DISK_ITEM 1
#define RAM_ITEM 2
#define PLAY_MENU 204
#define WINDOWS_MENU 205
#define FRONT_ITEM 1
#define REAR_ITEM 2
#define MOUSEFLAG_ITEM 4
#define RATE_MENU 206
#define R22K_ITEM 1
#define R11K_ITEM 2
#define R7K_ITEM 3
#define R5K_ITEM 4
/* *** Dialog Definitions *** */
#define FRONT_DIALOG 30
#define ST_ITEM 4
#define FL_ITEM 6
#define MW_ITEM 8
#define REAR_DIALOG 31
#define QT_ITEM 4
#define RL_ITEM 6
#define OK 1
#define CANCEL 2
#define ABOUT_DIALOG 256
#define OPENING_DIALOG 257
#define RAMREC_DIALOG 258
#define DISKREC_DIALOG 259
#define SID_ALERT 256
#define RECORD_PICT 258
/* *** Other Definitions *** */
#define BUFFERSIZE 100000L
/* *** Globals *** */
unsigned char *Buffer; /* address of buffer to use for sound data */
long BufLen; /* length of Buffer */
long RecordLen; /* length of recorded data */
Boolean MouseFlag; /* TRUE if mouse movement stops recording */
Boolean SIDStatus; /* TRUE if SID is responding and OK */
int SampleRate; /* sample recording rate */
int FrontStartThresh; /* front window starting threshold */
int FrontLength; /* front window length */
int FrontMaxWait; /* front window max wait */
int RearQuietThresh; /* rear window quiet threshold */
int RearLength; /* rear window length */
main()
{
int i;
int code;
int theMenu,theItem;
char ch;
long mResult;
EventRecord event;
WindowPtr whichWindow;
Boolean doneFlag;
/*****
* Do Mac initialization things...
*
*/
MaxApplZone();
MoreMasters();
InitGraf(&thePort);
InitFonts();
InitWindows();
InitMenus();
TEInit();
InitDialogs(0L);
FlushEvents(everyEvent,0);
InitCursor();
MenuInit(); /* create our menus */
/*****
* Initial front and rear window parameters
*
*/
FrontStartThresh = 0; /* no front window */
FrontLength = 0; /* ... */
FrontMaxWait = 300; /* timeout after 300K samples without sound */
RearQuietThresh = 0; /* no rear window - end recording by mouse movement */
RearLength = 0; /* ... */
SampleRate = 2; /* default to 11K sampling rate */
MouseFlag = TRUE; /* default to "Interrupt on mouse" checked */
DoOpeningBox(); /* thank you Eric for your suggestions... */
/*****
* Set up initial menu checks and disables...
*
*/
CheckItem(GetMenu(RATE_MENU),SampleRate,TRUE);
CheckItem(GetMenu(WINDOWS_MENU),MOUSEFLAG_ITEM,TRUE);
DisableItem(GetMenu(PLAY_MENU),RAM_ITEM);
/*****
* Create the fixed size buffer. Just give up if there
* isn't RAM available for the buffer.
*
*/
Buffer = (unsigned char *) NewPtr(BUFFERSIZE);
if (Buffer == 0L) {
ParamText("\pCannot allocate RAM","","","");
Alert(SID_ALERT,0L);
ExitToShell();
}
BufLen = BUFFERSIZE;
doneFlag = FALSE; /* We've Only Just Begun... */
while (doneFlag == FALSE) {
/*****
* Check if SID is alive every time through the event loop.
* Note that this isn't really necessary - checking is only
* required right before recording.
*
*/
SIDSetup();
SIDStatus = (HHError() == 0);
/*****
* Standard event handler...
*
*/
if (GetNextEvent(everyEvent,&event)) {
code = FindWindow(event.where,&whichWindow);
switch (event.what) {
case mouseDown:
if (code == inMenuBar) {
mResult = MenuSelect(event.where);
theMenu = HiWord(mResult);
theItem = LoWord(mResult);
HandleMenu(&doneFlag,theMenu,theItem);
}
else if (code == inContent) {
if (whichWindow != FrontWindow())
SelectWindow(whichWindow);
}
else if (code == inSysWindow)
SystemClick(&event,whichWindow);
break;
case keyDown:
case autoKey:
ch = event.message & charCodeMask;
if (event.modifiers & cmdKey) {
mResult = MenuKey(ch);
theMenu = HiWord(mResult);
if (theMenu != 0) {
theItem = LoWord(mResult);
HandleMenu(&doneFlag,theMenu,theItem);
}
}
break;
case updateEvt:
whichWindow = (WindowPtr) event.message;
BeginUpdate(whichWindow);
EndUpdate(whichWindow);
break;
case activateEvt:
whichWindow = (WindowPtr) event.message;
if (event.modifiers & 0x01 == 1)
SelectWindow(whichWindow);
break;
}
}
}
/****
* Quit was chosen - let HearHere clean up SID,
* and exit...
*
*/
HHEnd();
}
/*****
* MenuInit -- builds the menu bar for the application
*
*/
MenuInit()
{
int i; /* general integer use */
Rect bounds;
Handle h;
ClearMenuBar();
for (i = APPLE_MENU; i <= RATE_MENU; i++) {
if (i == APPLE_MENU) {
AddResMenu(GetMenu(i),'DRVR'); /* add in DA's */
}
InsertMenu(GetMenu(i),0);
}
DrawMenuBar();
}
/*****
* SIDSetup -- initializes SID and sets all of the window and sampling parameters
*
*/
SIDSetup()
{
HHInit(1);
HHFrontWindow(FrontStartThresh,FrontLength,FrontMaxWait);
HHRearWindow(RearQuietThresh,RearLength);
HHSampleRate(SampleRate);
}
/*****
* HandleMenu -- handles all menu choices from the user
*
*/
HandleMenu(doneFlag,theMenu,theItem)
Boolean *doneFlag;
int theMenu,theItem;
{
Boolean daEdit; /* for SystemEdit result of DA edits */
Str255 deskAccName; /* name of requested desk accessory */
ControlHandle ctrl; /* used for setting control attributes */
GrafPtr savePort; /* saved port to bracket DA call... */
PenState penSave; /* saved pen parameters */
int i;
switch (theMenu) {
case APPLE_MENU:
if (theItem == ABOUT_ITEM)
DoAboutBox();
else {
GetPort(&savePort);
GetItem(GetMenu(APPLE_MENU),theItem,deskAccName);
OpenDeskAcc(deskAccName);
SetPort(savePort);
}
break;
case FILE_MENU:
*doneFlag = TRUE; /* Quit is the only File choice */
break;
case RECORD_MENU:
if (!SIDStatus) { /* complain is SID isn't alive */
ParamText("\pSID is not responding","","","");
Alert(SID_ALERT,0L);
break;
}
if (theItem == DISK_ITEM) {
HandleDiskRecord();
}
else
HandleRAMRecord();
break;
case PLAY_MENU:
if (theItem == DISK_ITEM) {
HandleDiskPlay();
}
else
HandleRAMPlay();
break;
case WINDOWS_MENU:
if (theItem == FRONT_ITEM) {
HandleFrontDialog();
}
else if (theItem == REAR_ITEM) {
HandleRearDialog();
}
else if (theItem == MOUSEFLAG_ITEM) {
if (MouseFlag == TRUE) {
MouseFlag = FALSE;
CheckItem(GetMenu(WINDOWS_MENU),MOUSEFLAG_ITEM,FALSE);
}
else {
MouseFlag = TRUE;
CheckItem(GetMenu(WINDOWS_MENU),MOUSEFLAG_ITEM,TRUE);
}
}
break;
case RATE_MENU:
HHSampleRate(theItem);
SampleRate = theItem;
for (i = 1; i < 5; i++)
CheckItem(GetMenu(RATE_MENU),i,FALSE);
CheckItem(GetMenu(RATE_MENU),SampleRate,TRUE);
break;
}
HiliteMenu(0);
}
/*****
* HandleRAMRecord -- record from SID to a RAM buffer
*
*/
HandleRAMRecord()
{
EnableItem(GetMenu(PLAY_MENU),RAM_ITEM);
RecordDialog(RAMREC_DIALOG,TRUE); /* display the RECORDING dialog */
if (!HHRecord(Buffer,BufLen,0,MouseFlag,&RecordLen) && HHError() != INTERRUPT) {
ParamText("\pRecording Error","","","");
Alert(SID_ALERT,0L); /* this is pretty poor error checking... */
/* ...check HHError() for an error code... */
/* ...and look up that error code in HearHere.h */
}
else if (RecordLen == -1L) /* -1 means that the buffer was filled */
RecordLen = BufLen; /* ...remember the real length recorded */
RecordDialog(RAMREC_DIALOG,FALSE); /* remove the RECORDING dialog */
}
HandleDiskRecord()
{
int refNum;
Boolean b;
Point sfPt;
SFReply reply;
OSErr err;
/*****
* Ask the user for a filename to record to
*
*/
SetPt(&sfPt,80,90);
SFPutFile(sfPt,"","",0L,&reply);
if (!reply.good)
return;
err = Create(reply.fName,reply.vRefNum,'SID ','DATA');
if (err == -48) /* ignore "File Exists" error */
err = 0;
DisableItem(GetMenu(PLAY_MENU),RAM_ITEM); /* this will kill the RAM buffer */
err |= FSOpen(reply.fName,reply.vRefNum,&refNum);
if (err != noErr) {
ParamText("\pError opening file...","","","");
Alert(SID_ALERT,0L);
return;
}
SetEOF(refNum,0L); /* compact the file... */
/*****
* Record to the open file
*
*/
RecordDialog(DISKREC_DIALOG,TRUE); /* display the RECORDING dialog */
if (!HHRecord(Buffer,BufLen,refNum,MouseFlag,&RecordLen) && HHError() != INTERRUPT) {
ParamText("\pRecording Error","","","");
Alert(SID_ALERT,0L);
}
RecordDialog(DISKREC_DIALOG,FALSE); /* remove the RECORDING dialog */
FSClose(refNum); /* close the file */
}
/*****
* RecordDialog -- handle the RECORDING dialog box
*
*/
RecordDialog(whichOne,display)
int whichOne;
Boolean display;
{
int item,x,y;
long startTicks,time;
static DialogPtr d;
static GrafPtr saveport;
PicHandle h;
Str255 t;
Rect r;
if (display) {
GetPort(&saveport);
x = ((screenBits.bounds.left + screenBits.bounds.right) >> 1) - 125;
y = ((screenBits.bounds.top + screenBits.bounds.bottom) >> 1) - 92;
d = GetNewDialog(whichOne,0L,-1L);
h = GetPicture(RECORD_PICT);
SetRect(&r,36,57,215,127);
time = ((long) BUFFERSIZE * (long) SampleRate / 22254L) + 1L; /* just approx */
NumToString(time,t);
ParamText(t,"","","");
MoveWindow(d,x,y,FALSE);
if (d != 0L) {
SetPort(d);
ShowWindow(d);
DrawDialog(d);
startTicks = Ticks;
while (Ticks - startTicks < 180)
;
DrawPicture(h,&r);
}
}
else {
if (d != 0L)
DisposDialog(d);
FlushEvents(everyEvent,0);
SetPort(saveport);
}
}
/*****
* HandleRAMPlay -- play back the RAM recording
* Note: this demonstrates how to use the standard Mac toolbox to play back
* recordings done with HearHere.
*
*/
HandleRAMPlay()
{
long len;
FFSynthPtr b;
OSErr err;
StopSound(); /* kill any existing sound before playing */
/*****
* This will build a FFSynthRec right into the sound buffer
* which will kill the first few bytes of sound data. A better
* approach would be to copy the sound data into a new FFSynthRec
* but that would require more RAM.
*
*/
b = (FFSynthPtr) Buffer;
b->mode = ffMode; /* SID data is in ffMode format */
b->count = FixRatio(1,SampleRate); /* this sets the sample rate for playback */
SetSoundVol(7); /* set the sound level to the max... */
StartSound(b,RecordLen-10,-1L); /* the Mac ROM's do all the hard work! */
}
/*****
* HandleDiskPlay -- play back a disk recording
*
*/
HandleDiskPlay()
{
int refNum;
Point sfPt;
long sfType;
SFReply reply;
OSErr err;
/*****
* Ask the user for a filename to play
*
*/
SetPt(&sfPt,80,90);
sfType = (long) 'DATA';
SFGetFile(sfPt,"",0L,1,&sfType,0L,&reply);
if (!reply.good)
return;
err = FSOpen(reply.fName,reply.vRefNum,&refNum);
if (err != noErr) {
ParamText("\pError opening file...","","","");
Alert(SID_ALERT,0L);
return;
}
SetFPos(refNum,1,0L);
DisableItem(GetMenu(PLAY_MENU),RAM_ITEM); /* this will kill the RAM buffer */
/*****
* Use HearHere to playback the data from the opened file...
*
*/
if (!HHDiskPlay(refNum,Buffer,BufLen)) {
ParamText("\pError playing sound...","","","");
Alert(SID_ALERT,0L);
}
FSClose(refNum); /* close the file */
}
/*****
* AboutFilter -- just waits around for a mouse or key press
*
*/
pascal Boolean AboutFilter(d,event,item)
DialogPtr d;
EventRecord *event;
int *item;
{
if (event->what == mouseDown || event->what == keyDown) {
*item = 1;
return TRUE;
}
*item = 0;
return FALSE;
}
/*****
* DoAboutBox -- display the about box
*
*/
DoAboutBox()
{
int item,x,y;
DialogPtr d;
GrafPtr saveport;
GetPort(&saveport);
x = ((screenBits.bounds.left + screenBits.bounds.right) >> 1) - 142;
y = ((screenBits.bounds.top + screenBits.bounds.bottom) >> 1) - 85;
d = GetNewDialog(ABOUT_DIALOG,0L,-1L);
MoveWindow(d,x,y,FALSE);
if (d != 0L) {
SetPort(d);
ShowWindow(d);
do {
ModalDialog(AboutFilter,&item);
}
while (item == 0);
DisposDialog(d);
FlushEvents(everyEvent,0);
SetPort(saveport);
}
}
/*****
* DoOpeningBox -- same as about box, but draws the opening dialog box
*
*/
DoOpeningBox()
{
int item,x,y;
DialogPtr d;
GrafPtr saveport;
GetPort(&saveport);
x = ((screenBits.bounds.left + screenBits.bounds.right) >> 1) - 184;
y = ((screenBits.bounds.top + screenBits.bounds.bottom) >> 1) - 143;
d = GetNewDialog(OPENING_DIALOG,0L,-1L);
MoveWindow(d,x,y,FALSE);
if (d != 0L) {
SetPort(d);
ShowWindow(d);
do {
ModalDialog(AboutFilter,&item);
}
while (item == 0);
DisposDialog(d);
FlushEvents(everyEvent,0);
SetPort(saveport);
}
}
/*****
* HandleFrontDialog -- handles the filling out of the front window dialog
*
*/
HandleFrontDialog()
{
int iType,item;
long num;
Handle stHdl,flHdl,mwHdl;
Str255 stStr,flStr,mwStr;
Rect iRect;
DialogPtr theDialog;
theDialog = GetNewDialog(FRONT_DIALOG,0L,-1L);
GetDItem(theDialog,ST_ITEM,&iType,&stHdl,&iRect);
NumToString((long) FrontStartThresh,&stStr);
SetIText(stHdl,stStr);
SelIText(theDialog,ST_ITEM,0,32767);
GetDItem(theDialog,FL_ITEM,&iType,&flHdl,&iRect);
NumToString((long) FrontLength,&flStr);
SetIText(flHdl,flStr);
GetDItem(theDialog,MW_ITEM,&iType,&mwHdl,&iRect);
NumToString((long) FrontMaxWait,&mwStr);
SetIText(mwHdl,mwStr);
item = 0;
while (item != OK && item != CANCEL)
ModalDialog(0L,&item);
if (item == OK) {
GetIText(stHdl,&stStr);
GetIText(flHdl,&flStr);
GetIText(mwHdl,&mwStr);
StringToNum(stStr,&num);
FrontStartThresh = num;
StringToNum(flStr,&num);
FrontLength = num;
StringToNum(mwStr,&num);
FrontMaxWait = num;
HHFrontWindow(FrontStartThresh,FrontLength,FrontMaxWait);
}
DisposDialog(theDialog);
}
/*****
* HandleRearDialog -- handles the filling out of the rear window dialog
*
*/
HandleRearDialog()
{
int iType,item;
long num;
Handle qtHdl,rlHdl;
Str255 qtStr,rlStr;
Rect iRect;
DialogPtr theDialog;
theDialog = GetNewDialog(REAR_DIALOG,0L,-1L);
GetDItem(theDialog,QT_ITEM,&iType,&qtHdl,&iRect);
NumToString((long) RearQuietThresh,&qtStr);
SetIText(qtHdl,qtStr);
SelIText(theDialog,QT_ITEM,0,32767);
GetDItem(theDialog,RL_ITEM,&iType,&rlHdl,&iRect);
NumToString((long) RearLength,&rlStr);
SetIText(rlHdl,rlStr);
item = 0;
while (item != OK && item != CANCEL)
ModalDialog(0L,&item);
if (item == OK) {
GetIText(qtHdl,&qtStr);
GetIText(rlHdl,&rlStr);
StringToNum(qtStr,&num);
RearQuietThresh = num;
StringToNum(rlStr,&num);
RearLength = num;
HHRearWindow(RearQuietThresh,RearLength);
}
DisposDialog(theDialog);
}